/*
 * SearchPanel.java
 *
 * Created on June 27, 2005, 12:25 PM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.slimserver.music.MusicObjects;
import com.bff.bjj.ui.component.ColorProperties;
import com.bff.slimserver.music.exceptions.PlaylistException;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.ui.components.AlbumList;
import com.bff.slimserver.musicjukebox.ui.components.ArtistList;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.bjj.utils.Resources;
import com.bff.bjj.utils.Resources.LABEL_KEY;
import com.bff.slimserver.musicjukebox.ui.components.RoundedTextField;
import com.bff.slimserver.musicjukebox.ui.components.SongList;
import com.bff.bjj.utils.Utils;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistAddAlbumAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistAddArtistAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistAddSongAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistInsertAlbumAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistInsertArtistAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistInsertSongAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistLoadAlbumAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistLoadArtistAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistLoadSongAction;
import com.bff.slimserver.musicjukebox.lib.actions.ViewAlbumAction;
import com.bff.slimserver.musicjukebox.lib.actions.ViewArtistAction;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ListCellRenderer;
import org.bff.slimserver.SlimDatabase;
import org.bff.slimserver.SlimPlaylist;
import org.bff.slimserver.SlimSearchResult;
import org.bff.slimserver.exception.SlimException;
import org.bff.slimserver.musicobjects.SlimAlbum;
import org.bff.slimserver.musicobjects.SlimArtist;
import org.bff.slimserver.musicobjects.SlimContributor;
import org.bff.slimserver.musicobjects.SlimSong;

/**
 *
 * @author  Bill
 */
public class SearchPanel extends BJJPanel {

    private SlimDatabase database;
    private SlimPlaylist playlist;
    private SplashScreen sp;
    private SongListTablePanel songListTablePanel;
    private boolean loading;
    private PlaylistAddArtistAction artistAddAction;
    private PlaylistInsertArtistAction artistInsertAction;
    private PlaylistLoadArtistAction artistLoadAction;
    private ViewArtistAction artistViewSongsAction;
    private PlaylistAddAlbumAction albumAddAction;
    private PlaylistInsertAlbumAction albumInsertAction;
    private PlaylistLoadAlbumAction albumLoadAction;
    private ViewAlbumAction albumViewSongsAction;
    private PlaylistAddSongAction songAddAction;
    private PlaylistInsertSongAction songInsertAction;
    private PlaylistLoadSongAction songLoadAction;

    /** Creates new form SearchPanel */
    public SearchPanel() {
        super();
        try {
            this.database = JukeboxProperties.getInstance().getDatabase();
            this.playlist = JukeboxProperties.getInstance().getPlaylist();
        } catch (SlimException ex) {
            Logger.getLogger(SearchPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (PlaylistException ex) {
            Logger.getLogger(SearchPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        createActions();
        setArtistActionsEnabled(false);
        setAlbumActionsEnabled(false);
        setSongActionsEnabled(false);

        initComponents();

        getArtistList().setShowArtistonDoubleClick(false);
        getAlbumList().setShowArtistonDoubleClick(false);

        songListTablePanel = new SongListTablePanel();
        jPanel2.add(songListTablePanel, BorderLayout.CENTER);
    }

    private SongList getSongList() {
        return (SongList) songList;
    }

    private ArtistList getArtistList() {
        return (ArtistList) artistList;
    }

    private AlbumList getAlbumList() {
        return (AlbumList) albumList;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        mainPanel = new com.bff.bjj.ui.component.BJJPanel();
        jPanel1 = new com.bff.bjj.ui.component.BJJPanel();
        mainSearchPanel = new com.bff.bjj.ui.component.BJJPanel();
        tfSearchCriteria = new RoundedTextField();
        btnSearch = new javax.swing.JButton();
        btnSearch.setText(null);
        btnSearch.setContentAreaFilled(false);
        albumArtistPanel = new com.bff.bjj.ui.component.BJJPanel();
        artistPanel = new com.bff.bjj.ui.component.BJJPanel();
        artistButtonPanel = new com.bff.bjj.ui.component.BJJPanel();
        btnAddArtist = new com.bff.bjj.ui.component.BJJButton();
        btnInsertArtist = new com.bff.bjj.ui.component.BJJButton();
        btnLoadArtist = new com.bff.bjj.ui.component.BJJButton();
        lblArtist = new javax.swing.JLabel();
        btnViewArtistSongs = new com.bff.bjj.ui.component.BJJButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        artistList = new ArtistList();
        albumPanel = new com.bff.bjj.ui.component.BJJPanel();
        albumButtonPanel = new com.bff.bjj.ui.component.BJJPanel();
        lblAlbum = new javax.swing.JLabel();
        btnAddAlbum = new com.bff.bjj.ui.component.BJJButton();
        btnInsertAlbum = new com.bff.bjj.ui.component.BJJButton();
        btnLoadAlbum = new com.bff.bjj.ui.component.BJJButton();
        btnViewAlbumSongs = new com.bff.bjj.ui.component.BJJButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        albumList = new AlbumList();
        songPanel = new com.bff.bjj.ui.component.BJJPanel();
        songButtonPanel1 = new com.bff.bjj.ui.component.BJJPanel();
        btnAddArtist1 = new com.bff.bjj.ui.component.BJJButton();
        btnInsertArtist1 = new com.bff.bjj.ui.component.BJJButton();
        btnLoadArtist1 = new com.bff.bjj.ui.component.BJJButton();
        lblArtist1 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        songList = new SongList();
        jPanel2 = new com.bff.bjj.ui.component.BJJPanel();

        setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        setLayout(new java.awt.BorderLayout());

        mainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(getResource(LABEL_KEY.SEARCH_PANEL_BORDER_SEARCH)));
        mainPanel.setLayout(new java.awt.BorderLayout());

        jPanel1.setPreferredSize(new java.awt.Dimension(350, 586));
        jPanel1.setLayout(new java.awt.BorderLayout());

        mainSearchPanel.setPreferredSize(new java.awt.Dimension(300, 40));

        tfSearchCriteria.setColumns(24);
        tfSearchCriteria.setMinimumSize(new java.awt.Dimension(200, 25));
        tfSearchCriteria.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfSearchCriteriaKeyReleased(evt);
            }
        });
        mainSearchPanel.add(tfSearchCriteria);

        btnSearch.setBackground(java.awt.Color.lightGray);
        btnSearch.setIcon(getIcon(Resources.ICON_KEY.ICON_SEARCH));
        btnSearch.setBorder(null);
        btnSearch.setFocusPainted(false);
        btnSearch.setMaximumSize(new java.awt.Dimension(50, 50));
        btnSearch.setMinimumSize(new java.awt.Dimension(17, 17));
        btnSearch.setPreferredSize(new java.awt.Dimension(17, 17));
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        mainSearchPanel.add(btnSearch);

        jPanel1.add(mainSearchPanel, java.awt.BorderLayout.NORTH);

        albumArtistPanel.setLayout(new javax.swing.BoxLayout(albumArtistPanel, javax.swing.BoxLayout.PAGE_AXIS));

        artistPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(getResource(LABEL_KEY.SEARCH_PANEL_BORDER_ARTIST)));
        artistPanel.setLayout(new java.awt.BorderLayout());

        artistButtonPanel.setLayout(new java.awt.GridBagLayout());

        btnAddArtist.setAction(artistAddAction);
        btnAddArtist.setBorder(null);
        btnAddArtist.setPreferredSize(new java.awt.Dimension(75, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        artistButtonPanel.add(btnAddArtist, gridBagConstraints);

        btnInsertArtist.setAction(artistInsertAction);
        btnInsertArtist.setBorder(null);
        btnInsertArtist.setPreferredSize(new java.awt.Dimension(75, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        artistButtonPanel.add(btnInsertArtist, gridBagConstraints);

        btnLoadArtist.setAction(artistLoadAction);
        btnLoadArtist.setBorder(null);
        btnLoadArtist.setPreferredSize(new java.awt.Dimension(75, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        artistButtonPanel.add(btnLoadArtist, gridBagConstraints);

        lblArtist.setForeground(ColorProperties.INFO_MESSAGE_FORGROUND);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 4;
        artistButtonPanel.add(lblArtist, gridBagConstraints);

        btnViewArtistSongs.setAction(artistViewSongsAction);
        btnViewArtistSongs.setPreferredSize(new java.awt.Dimension(75, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        artistButtonPanel.add(btnViewArtistSongs, gridBagConstraints);

        artistPanel.add(artistButtonPanel, java.awt.BorderLayout.SOUTH);

        artistList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        artistList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                artistListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(artistList);

        artistPanel.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        albumArtistPanel.add(artistPanel);

        albumPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(getResource(LABEL_KEY.SEARCH_PANEL_BORDER_ALBUM)));
        albumPanel.setLayout(new java.awt.BorderLayout());

        albumButtonPanel.setLayout(new java.awt.GridBagLayout());

        lblAlbum.setForeground(ColorProperties.INFO_MESSAGE_FORGROUND);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        albumButtonPanel.add(lblAlbum, gridBagConstraints);

        btnAddAlbum.setAction(albumAddAction);
        btnAddAlbum.setBorder(null);
        btnAddAlbum.setPreferredSize(new java.awt.Dimension(75, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        albumButtonPanel.add(btnAddAlbum, gridBagConstraints);

        btnInsertAlbum.setAction(albumInsertAction);
        btnInsertAlbum.setBorder(null);
        btnInsertAlbum.setPreferredSize(new java.awt.Dimension(75, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        albumButtonPanel.add(btnInsertAlbum, gridBagConstraints);

        btnLoadAlbum.setAction(albumLoadAction);
        btnLoadAlbum.setBorder(null);
        btnLoadAlbum.setPreferredSize(new java.awt.Dimension(75, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        albumButtonPanel.add(btnLoadAlbum, gridBagConstraints);

        btnViewAlbumSongs.setAction(albumViewSongsAction);
        btnViewAlbumSongs.setBorder(null);
        btnViewAlbumSongs.setPreferredSize(new java.awt.Dimension(75, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        albumButtonPanel.add(btnViewAlbumSongs, gridBagConstraints);

        albumPanel.add(albumButtonPanel, java.awt.BorderLayout.SOUTH);

        albumList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        albumList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                albumListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(albumList);

        albumPanel.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        albumArtistPanel.add(albumPanel);

        songPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(getResource(LABEL_KEY.SEARCH_PANEL_BORDER_SONG)));
        songPanel.setLayout(new java.awt.BorderLayout());

        songButtonPanel1.setLayout(new java.awt.GridBagLayout());

        btnAddArtist1.setAction(songAddAction);
        btnAddArtist1.setBorder(null);
        btnAddArtist1.setPreferredSize(new java.awt.Dimension(75, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        songButtonPanel1.add(btnAddArtist1, gridBagConstraints);

        btnInsertArtist1.setAction(songInsertAction);
        btnInsertArtist1.setBorder(null);
        btnInsertArtist1.setPreferredSize(new java.awt.Dimension(75, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        songButtonPanel1.add(btnInsertArtist1, gridBagConstraints);

        btnLoadArtist1.setAction(songLoadAction);
        btnLoadArtist1.setBorder(null);
        btnLoadArtist1.setPreferredSize(new java.awt.Dimension(75, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        songButtonPanel1.add(btnLoadArtist1, gridBagConstraints);

        lblArtist1.setForeground(ColorProperties.INFO_MESSAGE_FORGROUND);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 4;
        songButtonPanel1.add(lblArtist1, gridBagConstraints);

        songPanel.add(songButtonPanel1, java.awt.BorderLayout.SOUTH);

        songList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        songList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                songListValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(songList);

        songPanel.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        albumArtistPanel.add(songPanel);

        jPanel1.add(albumArtistPanel, java.awt.BorderLayout.CENTER);

        mainPanel.add(jPanel1, java.awt.BorderLayout.WEST);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(getResource(LABEL_KEY.SEARCH_PANEL_BORDER_SONG_RESULTS)));
        jPanel2.setLayout(new java.awt.BorderLayout());
        mainPanel.add(jPanel2, java.awt.BorderLayout.CENTER);

        add(mainPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void tfSearchCriteriaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfSearchCriteriaKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            startSearch();
        }
    }//GEN-LAST:event_tfSearchCriteriaKeyReleased

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
    startSearch();
}//GEN-LAST:event_btnSearchActionPerformed

private void artistListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_artistListValueChanged
    if (!isLoading()) {
        setLoading(true);
        getAlbumList().clearSelection();
        getSongList().clearSelection();

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    getSongListTablePanel().setProgressBarVisible(true);
                    getSongListTablePanel().setArtist(getArtistList().getSelectedArtist());
                    if (getArtistList().getSelectedArtist() == null) {
                        setArtistActionsEnabled(false);
                    } else {
                        setArtistActionsEnabled(true);
                        setAlbumActionsEnabled(false);
                        setSongActionsEnabled(false);
                        setArtistActions(getArtistList().getSelectedArtist());
                    }
                } finally {
                    setLoading(false);
                    getSongListTablePanel().setProgressBarVisible(false);
                }
            }
        }).start();

    }
}//GEN-LAST:event_artistListValueChanged

private void albumListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_albumListValueChanged
    if (!isLoading()) {
        setLoading(true);
        getArtistList().clearSelection();
        getSongList().clearSelection();

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    getSongListTablePanel().setProgressBarVisible(true);
                    getSongListTablePanel().setAlbum(getAlbumList().getSelectedAlbum());
                    if (getAlbumList().getSelectedAlbum() == null) {
                        setAlbumActionsEnabled(false);
                    } else {
                        setAlbumActionsEnabled(true);
                        setArtistActionsEnabled(false);
                        setSongActionsEnabled(false);
                        setAlbumActions(getAlbumList().getSelectedAlbum());
                    }
                } finally {
                    setLoading(false);
                    getSongListTablePanel().setProgressBarVisible(false);
                }
            }
        }).start();
    }
}//GEN-LAST:event_albumListValueChanged

private void songListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_songListValueChanged
    if (!isLoading()) {
        setLoading(true);
        getAlbumList().clearSelection();
        getArtistList().clearSelection();
        try {
            getSongListTablePanel().setProgressBarVisible(true);
            if (getSongList().getSelectedSong() == null) {
                setSongActionsEnabled(false);
            } else {
                setSongActionsEnabled(true);
                setAlbumActionsEnabled(false);
                setArtistActionsEnabled(false);
                setSongActions(getSongList().getSelectedSong());
                List<SlimSong> sl = new ArrayList<SlimSong>();
                sl.add(getSongList().getSelectedSong());
                getSongListTablePanel().setSongs(sl);
            }
        } finally {
            getSongListTablePanel().setProgressBarVisible(false);
            getSongListTablePanel().repaint();
            setLoading(false);
        }
    }
}//GEN-LAST:event_songListValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel albumArtistPanel;
    private javax.swing.JPanel albumButtonPanel;
    private javax.swing.JList albumList;
    private javax.swing.JPanel albumPanel;
    private javax.swing.JPanel artistButtonPanel;
    private javax.swing.JList artistList;
    private javax.swing.JPanel artistPanel;
    private javax.swing.JButton btnAddAlbum;
    private javax.swing.JButton btnAddArtist;
    private javax.swing.JButton btnAddArtist1;
    private javax.swing.JButton btnInsertAlbum;
    private javax.swing.JButton btnInsertArtist;
    private javax.swing.JButton btnInsertArtist1;
    private javax.swing.JButton btnLoadAlbum;
    private javax.swing.JButton btnLoadArtist;
    private javax.swing.JButton btnLoadArtist1;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnViewAlbumSongs;
    private javax.swing.JButton btnViewArtistSongs;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lblAlbum;
    private javax.swing.JLabel lblArtist;
    private javax.swing.JLabel lblArtist1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel mainSearchPanel;
    private javax.swing.JPanel songButtonPanel1;
    private javax.swing.JList songList;
    private javax.swing.JPanel songPanel;
    private javax.swing.JTextField tfSearchCriteria;
    // End of variables declaration//GEN-END:variables

    private void search() {
        setLoading(true);
        try {
            this.setCursor(Utils.getHourglassCursor());
            String searchText = tfSearchCriteria.getText().trim();
            if ("".equals(searchText)) {
                searchText = null;
            }

            SlimSearchResult searchResult = getDatabase().searchAll(searchText);
            getSongList().setSongs(searchResult.getSongs());

            //for now only artists
            List<SlimArtist> artists = new ArrayList<SlimArtist>();
            for (SlimArtist artist : searchResult.getArtists()) {
                SlimArtist a = MusicObjects.getArtistMap().get(artist.getId());
                if (a == null) {
                    artists.add(artist);
                } else {
                    artists.add(a);
                }
            }
            for (SlimContributor cont : searchResult.getContributors()) {
                SlimArtist a = MusicObjects.getArtistMap().get(cont.getId());
                if (a == null) {
                    SlimArtist artist = new SlimArtist();
                    artist.setId(cont.getId());
                    artist.setName(cont.getName());
                    artists.add(artist);
                } else {
                    artists.add(a);
                }

            }

            getArtistList().setArtist(artists);


            List<SlimAlbum> albums = new ArrayList<SlimAlbum>();
            for (SlimAlbum album : searchResult.getAlbums()) {
                SlimAlbum a = MusicObjects.getAlbumMap().get(album.getId());
                if (a == null) {
                    albums.add(album);
                } else {
                    albums.add(a);
                }
            }

            getAlbumList().setAlbums(albums);

            getAlbumList().clearSelection();
            getArtistList().clearSelection();
            getSongList().clearSelection();
        } catch (SlimException ex) {
            Logger.getLogger(SearchPanel.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            setLoading(false);
            this.setCursor(Utils.getNormalCursor());
        }

    }

    private void startSearch() {
        if (tfSearchCriteria.getText().trim() != null
                && !"".equalsIgnoreCase(tfSearchCriteria.getText().trim())) {
            sp = new SplashScreen(Utils.getParentComponent(this), true);
            sp.setIndeterminate(true);
            sp.setTitleLabel(getResource(LABEL_KEY.SEARCH_DIALOG_PROGRESS_TITLE));
            sp.setMainTextArea(getResource(LABEL_KEY.SEARCH_DIALOG_PROGRESS_TEXT));
            sp.setProgressBarLabel(getResource(LABEL_KEY.SEARCH_DIALOG_PROGRESS_LABEL));
            sp.pack();

            Thread t = new Thread(new SlimSearcher(this));
            t.start();
        }
    }

    public SlimDatabase getDatabase() {
        return database;
    }

    public void setDatabase(SlimDatabase database) {
        this.database = database;
    }

    public SlimPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(SlimPlaylist playlist) {
        this.playlist = playlist;
    }

    /**
     * @return the songListTablePanel
     */
    public SongListTablePanel getSongListTablePanel() {
        return songListTablePanel;
    }

    /**
     * @param songListTablePanel the songListTablePanel to set
     */
    public void setSongListTablePanel(SongListTablePanel songListTablePanel) {
        this.songListTablePanel = songListTablePanel;
    }

    /**
     * @return the loading
     */
    public boolean isLoading() {
        return loading;
    }

    /**
     * @param loading the loading to set
     */
    public void setLoading(boolean loading) {
        this.loading = loading;
    }

    private void createActions() {
        artistAddAction = new PlaylistAddArtistAction(playlist, null);
        artistInsertAction = new PlaylistInsertArtistAction(playlist, null);
        artistLoadAction = new PlaylistLoadArtistAction(playlist, null);
        artistViewSongsAction = new ViewArtistAction(Utils.getParentComponent(this), null);
        albumAddAction = new PlaylistAddAlbumAction(playlist, null);
        albumInsertAction = new PlaylistInsertAlbumAction(playlist, null);
        albumLoadAction = new PlaylistLoadAlbumAction(playlist, null);
        albumViewSongsAction = new ViewAlbumAction(Utils.getParentComponent(this), null);
        songAddAction = new PlaylistAddSongAction(playlist, null);
        songInsertAction = new PlaylistInsertSongAction(playlist, null);
        songLoadAction = new PlaylistLoadSongAction(playlist, null);
    }

    private void setArtistActionsEnabled(boolean enabled) {
        getArtistAddAction().setEnabled(enabled);
        getArtistInsertAction().setEnabled(enabled);
        getArtistLoadAction().setEnabled(enabled);
        getArtistViewSongsAction().setEnabled(enabled);
    }

    private void setArtistActions(SlimArtist selectedArtist) {
        getArtistAddAction().setArtist(selectedArtist);
        getArtistInsertAction().setArtist(selectedArtist);
        getArtistLoadAction().setArtist(selectedArtist);
        getArtistViewSongsAction().setArtist(selectedArtist);
    }

    private void setAlbumActionsEnabled(boolean enabled) {
        getAlbumAddAction().setEnabled(enabled);
        getAlbumInsertAction().setEnabled(enabled);
        getAlbumLoadAction().setEnabled(enabled);
        getAlbumViewSongsAction().setEnabled(enabled);
    }

    private void setAlbumActions(SlimAlbum selectedAlbum) {
        getAlbumAddAction().setAlbum(selectedAlbum);
        getAlbumInsertAction().setAlbum(selectedAlbum);
        getAlbumLoadAction().setAlbum(selectedAlbum);
        getAlbumViewSongsAction().setAlbum(selectedAlbum);
    }

    private void setSongActionsEnabled(boolean enabled) {
        getSongAddAction().setEnabled(enabled);
        getSongInsertAction().setEnabled(enabled);
        getSongLoadAction().setEnabled(enabled);
    }

    private void setSongActions(SlimSong song) {
        getSongAddAction().setSong(song);
        getSongInsertAction().setSong(song);
        getSongLoadAction().setSong(song);
    }

    /**
     * @return the artistAddAction
     */
    private PlaylistAddArtistAction getArtistAddAction() {
        return artistAddAction;
    }

    /**
     * @return the artistInsertAction
     */
    private PlaylistInsertArtistAction getArtistInsertAction() {
        return artistInsertAction;
    }

    /**
     * @return the artistLoadAction
     */
    private PlaylistLoadArtistAction getArtistLoadAction() {
        return artistLoadAction;
    }

    /**
     * @return the artistViewSongsAction
     */
    private ViewArtistAction getArtistViewSongsAction() {
        return artistViewSongsAction;
    }

    /**
     * @return the albumAddAction
     */
    public PlaylistAddAlbumAction getAlbumAddAction() {
        return albumAddAction;
    }

    /**
     * @return the albumInsertAction
     */
    public PlaylistInsertAlbumAction getAlbumInsertAction() {
        return albumInsertAction;
    }

    /**
     * @return the albumLoadAction
     */
    public PlaylistLoadAlbumAction getAlbumLoadAction() {
        return albumLoadAction;
    }

    /**
     * @return the albumViewSongsAction
     */
    public ViewAlbumAction getAlbumViewSongsAction() {
        return albumViewSongsAction;
    }

    /**
     * @return the songAddAction
     */
    private PlaylistAddSongAction getSongAddAction() {
        return songAddAction;
    }

    /**
     * @return the songInsertAction
     */
    private PlaylistInsertSongAction getSongInsertAction() {
        return songInsertAction;
    }

    /**
     * @return the songLoadAction
     */
    private PlaylistLoadSongAction getSongLoadAction() {
        return songLoadAction;
    }

    private class SlimSearcher implements Runnable {

        private Component parent;

        public SlimSearcher(Component parent) {
            this.parent = parent;
        }

        @Override
        public void run() {
            try {
                search();
                sp.setStopped(true);
            } catch (Exception e) {
                new ErrorDialog((JFrame) Utils.getParentComponent(parent), "No Search Results", null, false);
            } finally {
                sp.setStopped(true);
            }
        }
    }
}
